package io.github.hadyang.leetcode.offer;

import org.junit.Test;

/** @author haoyang.shi */
public class LeftRotateString {

  @Test
  public void test() {
    System.out.println(LeftRotateString("abcXYZdef", 3));
  }

  public String LeftRotateString(String str, int n) {
    if (str == null || str.trim().equals("")) return str;

    String res = revert(str, 0, n - 1);
    res = revert(res, n, str.length() - 1);
    res = revert(res, 0, str.length() - 1);

    return res;
  }

  private String revert(String str, int start, int end) {
    char[] chars = str.toCharArray();

    while (start < end) {
      char t = chars[start];
      chars[start] = chars[end];
      chars[end] = t;

      start++;
      end--;
    }

    return new String(chars);
  }
}
